﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class DelForm : Form
    {
        ListViewItem delItem = null;
        Monitor_DB del_db = Monitor_DB.instance;
        sensor_type del_sensorType;
        ListView parent_listView1 = null;

        public DelForm(ListViewItem item, sensor_type s_type, MonitorForm parent)
        {
            InitializeComponent();

            delItem = item;
            del_sensorType = s_type;

            if (parent != null)
            {
                parent_listView1 = parent.Get_MonitorForm_ListView1();
            }
        }

        private void DelForm_Load(object sender, EventArgs e)
        {

        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        public void deleteItem()
        {
            try
            {
                if (delItem != null)
                {
                    switch (del_sensorType)
                    {
                        case sensor_type.door:

                            var result =
                                from del in del_db.db.tb_door
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            del_db.db.tb_door.DeleteAllOnSubmit(result);
                            del_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            break;

                        case sensor_type.tempture:

                            var tem =
                                from del in del_db.db.tb_tempture
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            del_db.db.tb_tempture.DeleteAllOnSubmit(tem);
                            del_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            break;

                        case sensor_type.humidity:

                            var hum =
                                from del in del_db.db.tb_humidity
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            del_db.db.tb_humidity.DeleteAllOnSubmit(hum);
                            del_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            break;

                        case sensor_type.shake:

                            var shake =
                                from del in del_db.db.tb_shake
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            del_db.db.tb_shake.DeleteAllOnSubmit(shake);
                            del_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            break;

                        case sensor_type.voltage:

                            var vol =
                                from del in del_db.db.tb_voltage
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            del_db.db.tb_voltage.DeleteAllOnSubmit(vol);
                            del_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            break;


                        default:
                            break;
                    }

                    MessageBox.Show("删除成功！");
                    parent_listView1.Items.Remove(delItem);

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
                del_db.Refresh_DB();
            }
        }

        private void button_enter_Click(object sender, EventArgs e)
        {
            deleteItem();
            this.Dispose();
        }
    }
}
